<script lang="ts" setup>
import { ref, computed } from 'vue'

// import { showToast } from 'vant';
// import {useUserStore} from '@/stores/user'
import { useAddressStore } from '@/stores/address'

import { onMounted } from 'vue'

import { useRouter,useRoute } from 'vue-router'
const router = useRouter()
const route = useRoute()
// const userStore = useUserStore()
const addressStore = useAddressStore()
const chosenAddressId = ref('1')

// const list = ref([])
const onClickLeft = () => history.back()
//编辑
const onEdit = (item: any) => {
  router.push(`/addressitem/?id=${item.id}`)
}

//获取地址列表
const newAddressStore = computed(() => {
  return addressStore.list.map((item: { id: any; address: any; province: any; city: any; county: any; addressDetail: any }) => {
    item.address = item.province + item.city + item.county + item.addressDetail
    return item
  })
})

// /切换选中的地址时触发
const onSelect = (ev: any) => {
  console.log(route.query.id);
  
  ev.isDefault=!ev.isDefault
  if(route.query.id){
    addressStore.isChecked(ev)
    router.push('/pay')
  }else{
    addressStore.isChecked(ev)
  }
 
 
  // console.log(ev.id)

  // getAddressList()
}

onMounted(() => {
  // getAddressList()
})
</script>

<template>
  <van-nav-bar title="地址管理" left-text="返回" left-arrow @click-left="onClickLeft" />
  <van-address-list v-model="chosenAddressId" :list="newAddressStore" default-tag-text="默认" @add="router.push('/addressitem')" @edit="onEdit" @select="onSelect" />
  
</template>

<style scoped lang="less">
:deep(.van-button--round) {
  padding: 0 16px;
}
</style>
